<?php


namespace app\admin\command;

use app\admin\model\Store;
use app\admin\model\Transaction;
use app\admin\model\user\Card;
use app\admin\model\Reserve;
use app\admin\model\Room;
use app\admin\model\user\Extend;
use app\common\library\Yunding;
use Carbon\Carbon;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Db;
use think\Env;
use think\Exception;
use think\Log;

class TransUserCard extends Command
{

    protected function configure()
    {
        $this->setName('trans_user_card')
            ->setDescription('把记录里user的card做一个转换');
    }

    protected function execute(Input $input, Output $output)
    {
        @ini_set("memory_limit", '-1');
        $stime = microtime(true);

//        $userOldId = 14;
//        $userNewId = 5169;

        $userOldId = 3605;
        $userNewId = 5168;

        $sql = "SELECT * FROM `dancestudio_firs`.`fa_user_card` WHERE `user_id` = $userOldId";
        $data = Db::query($sql);

        foreach ($data as $item) {
            Card::create([
               'user_id' => $userNewId,
                'card_id' => $item['card_id'],
                'name' => $item['name'],
                'type' => $item['type'],
                'price' => $item['price'],
                'service_start' => $item['service_start'],
                'service_end' => $item['service_end'],
                'store_ids' => $item['room_id'],
                'checknum' => $item['checknum'],
                'hascheckednum' => $item['hascheckednum'],
                'total_hour' => $item['total_hour'],
                'hasconsumedhour' => $item['hasconsumedhour'],
                'dzmt' => $item['dzmt'],
                'dzmt_code' => null,
                'transaction_id' => $item['transaction_id'],
                'createtime' => $item['createtime'],
                'updatetime' => $item['updatetime']
            ]);
        }

        $sql = "select * from `dancestudio_firs`.`fa_user_extend` WHERE `user_id` = $userOldId";
        $data = Db::query($sql);

        if (isset($data[0]) and isset($data[0]['balance'])) {
            $userExtend = Extend::where('user_id', $userNewId)->find();
            $userExtend->balance = $data[0]['balance'];
            $userExtend->save();
        }


        $etime = microtime(true);//获取程序执行结束的时间
        $total = $etime - $stime;   //计算差值
        $output->writeln($total);
    }

}
